/* 
 *  <copyright file="AttachedPositionUpdater.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using System.Text;


using Microsoft.Xna.Framework;
using SobrietyEngine.Common;

using SobrietyEngine.Guids;
using SobrietyEngine.Actor;
using SobrietyEngine.Components.Attach;
using SobrietyEngine.Components.Common;
using SobrietyEngine.Components.Common;

namespace SobrietyEngine.Components.Position
{
    public class AttachedPositionUpdater: AttachedToActor, SobrietyEngine.Components.IComponent
    {
        PositionCmp AttachedToPosition=null;
        PositionCmp MyPosition = null;

        RotationCmp AttachedToRotation = null;
        Vector2 RotatedAttachedTo;
        float curRot = 0.0f;

        Vector2 attachedToPoint;
        public Vector2 AttachedToPoint { get { return attachedToPoint; } set { attachedToPoint = value; } }

        Vector2 AttachedPoint;

        public AttachedPositionUpdater()
                : base()
       {
       }

        override public void Set(Parameters parms)
        {
            base.Set(parms);
            if (parms.ContainsKey("AttachedToPoint"))
                attachedToPoint = (Vector2)parms["AttachedToPoint"];
            else
                throw new Exception("Requires AttchedToPoint");

            if (parms.ContainsKey("AttachedPoint"))
                AttachedPoint = (Vector2)parms["AttachedPoint"];
            else
                throw new Exception("REquires attachedPoint");
        }

        override public Boolean InitComponent()
        {
            base.InitComponent();

            AttachedToPosition = AttachedTo.Get<IPosition>();
            if (AttachedToPosition == null)
                throw new Exception("Attached to actor required to have a position!");

            AttachedToRotation = (RotationCmp)AttachedTo[typeof(IRotation)];

            MyPosition = (PositionCmp)ParentActor[typeof(IPosition)];
            if (AttachedToPosition == null)
                throw new Exception("actor required to have a position!");

            RotatedAttachedTo = attachedToPoint;

            return true;
        }
        
       new public void Update(GameTime elapsedTime)
       {
           base.Update(elapsedTime);
           if (AttachedToRotation != null)
           {
               if (curRot != AttachedToRotation.GetRadians())
               {
                   curRot = AttachedToRotation.GetRadians();
                   Vector3 ro = AttachedToRotation.RotationOrigin;
                   RotatedAttachedTo = rotateAroundPoint(new Vector2(ro.X,ro.Y), ref curRot, ref attachedToPoint);
               }
           }
           MyPosition.Set(AttachedToPosition.Get() + RotatedAttachedTo - AttachedPoint);
       }

        public Vector2 rotateAroundPoint(Vector2 origin, ref float radians, ref Vector2 vec)
        {
            Matrix myRotationMatrix = Matrix.CreateRotationZ(radians);

            // Rotate relative to origin.
            return Vector2.Transform(vec - origin, myRotationMatrix) + origin;
        }
    }
}